<?php
/*
 * 功能说明：自定义变量
 *  
*/

namespace app\admin\controller;
use think\Db;
use think\View;
 class Variable extends Comm
 {
 	public function index()
 	{
 		$vars = Db::name('setting')->where('type=1')->select();
        $this->assign('vars', $vars);
        return $this->fetch('index');
 	}
 	//新增变量
 	public function add()
 	{
 		return $this->fetch('form');
 	}
 	//编辑
 	public function edit($k=null)
 	{
 		$var = Db::name('setting')->where("k='$k'")->find();
        if (!$var) {
            $this->error('参数错误！');
        }
       // var_dump($var);exit;
        $this->assign('var', $var);
        return $this->fetch('form');
 	}
 	public function del()
    {
        $k = input('param.k');
		//print_r($k);die;
        if ($k <> '') {
            if (Db::name('setting')->where('type',1)->where('k',$k)->delete()) {
                addlog('删除自定义变量，ID：' . $k);
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }
    public function update()
    {

        $data['k'] = input('post.k');
        $varname = input('post.varname');
        if ($data['k'] == '') {
            $this->error('变量名不能为空。');
        }
        if (Db::name('setting')->where("k='{$data['k']}'")->count() && $varname == '') {
            $this->error('变量名称已经存在。');
        }
        $data['v'] = input('post.v');
        $data['name'] = input('post.name');
        $data['type'] = 1;//自定义
        if ($varname == '') {
            Db::name('setting')->insert($data);
            addlog('新增自定义变量：' . $data['k']);
        } else {
			//dump($varname);die;
            Db::name('setting')->where('k',$varname)->update($data);
            addlog('新增自定义变量：' . $data['k']);
        }

        $this->success('恭喜，操作成功！', Url('index'));
    }

 }

?>